package com.zzg.datastructure.stack;

/**
 * @Author zhengzg
 * @Date 2022-08-18
 * @Version v1.0
 */
public class ArrayStack {

    /**
     * 栈的大小
     */
    private int maxSize;

    /**
     * 数组
     */
    private int[] stack;

    /**
     * 栈顶
     */
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[this.maxSize];
    }

    // 获取栈顶
    public int peek(){
        return stack[top];
    }

    // 栈满
    public boolean isFull(){
        return top == maxSize - 1;
    }

    // 栈空
    public boolean isEmpty(){
        return top == -1;
    }

    // 入栈 push
    public void push(int value){
        // 先判断栈是否满
        if (isFull()){
            System.out.println("沾满");
            return;
        }

        top++;
        stack[top] = value;
    }

    // 出栈
    public int pop(){
        // 先判断栈是否为空
        if (isEmpty()){
            throw new RuntimeException("栈空，没有数据");
        }

        int value = stack[top];
        top--;
        return value;
    }

    // 显示栈的情况遍历需要从栈顶显示数据
    public void list(){
        if (isEmpty()){
            System.out.println("栈空没有数据");
            return;
        }
        // 需要从栈顶开始显示数据
        for (int i = top; i >= 0 ; i--) {
            System.out.printf("Stack[%d]=%d\n",i,stack[i]);
        }
    }

    // 返回符号的优先级，优先级是程序员来确定优先级使用数字表示
    public int priority(int oper) {
        if (oper == '*' || oper == '/'){
            return 1;
        }else if (oper == '+' || oper == '-'){
            return 0;
        }else {
            return -1;
        }
    }

    // 判断是否为运算符
    public boolean isOper(char val){
        return val == '*' || val == '/' || val == '+' || val == '-';
    }

    // 计算方法
    public int cal(int num1,int num2,int oper){
        // 存放计算结果
        int res = 0;
        switch (oper){
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num1 - num2;
                break;
            case  '*':
                res = num1 * num2;
                break;
            case  '/':
                res = num1 / num2;
                break;
            default:
                break;
        }
        return res;
    }
}
